/*
 * @Author: jiu yin
 * @Date: 2023-05-11 13:52:59
 * @LastEditTime: 2023-06-06 14:06:59
 * @LastEditors: jiu yin zhen jing
 * @FilePath: \AI admin\src\utils\index.js
 * jiu
 */
/**
 * @description:  深拷贝
 * @return {*}
 */

export function deepClone(obj) {
    const typeOfStr = Object.prototype.toString;
    const objType = typeOfStr.call(obj).slice(8, -1);
    // console.log(obj);
    if (objType !== 'Object' && objType !== 'Array') return obj;

    let newObj = objType === 'Array' ? [] : {};

    if (objType == "Object") {
        for (let key in obj) {
            newObj[key] = deepClone(obj[key])
        }
    }

    if (objType == "Array") {
        obj.forEach((item, index) => {
            newObj.push(deepClone(item))
        })
    }


    return newObj;
}


//promise微任务
export function asyncFn(fn) {
    return Promise.resolve().then(fn)
}
//mutation微任务
export function mutationFn(fn) {
    let ob = new MutationObserver(fn)
    let text = document.createTextNode("0")
    ob.observe(text, { characterData: true })
    text.data = 1
}

/**
 * 格式化
 * @param { Date } day
 * @param { String } fmt
 * @returns { String } time
 */
export function parseTime(time, cFormat) {
    if (arguments.length === 0 || !time) {
        return null;
    }
    const format = cFormat || "{y}-{m}-{d} {h}:{i}:{s}";
    let date;
    if (typeof time === "object") {
        date = time;
    } else {
        if (typeof time === "string") {
            if (/^[0-9]+$/.test(time)) {
                // support "1548221490638"
                time = parseInt(time);
            } else {
                time = time.replace(new RegExp(/-/gm), "/");
            }
        }

        if (typeof time === "number" && time.toString().length === 10) {
            time = time * 1000;
        }
        date = new Date(time);
    }
    const formatObj = {
        y: date.getFullYear(),
        m: date.getMonth() + 1,
        d: date.getDate(),
        h: date.getHours(),
        i: date.getMinutes(),
        s: date.getSeconds(),
        a: date.getDay(),
    };
    const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
        const value = formatObj[key];
        // Note: getDay() returns 0 on Sunday
        if (key === "a") {
            return ["日", "一", "二", "三", "四", "五", "六"][value];
        }
        return value.toString().padStart(2, "0");
    });
    return time_str;
}


//判断数据类型
export function typeOf(obj) {
    const toString = Object.prototype.toString.call(obj);
    return toString.slice(8, -1);
}


// 获取路由传参
export function getParams() {
    const searchParams = new URLSearchParams(window.location.search);
    const params = {};
    for (const [key, value] of searchParams.entries()) {
        params[key] = value;
    }
    return params
}
//16进制颜色
export function getRandomHexColor() {
    return '#' + Math.floor(Math.random() * 0xFFFFFF).toString(16).padStart(6, '0');
}

//颜色数组
export function getColorArr() {
    return ['#f50', '#2db7f5', '#87d068', '#108ee9',]
}